﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace PizzaWic.customer
{
    public partial class DisplayBasket : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string[] dataString=new string[0];
            int sumCommand = 0;
            int quantiteProduct = 0;
            List<int> idProduct = (List<int>)Session["basket"];
            idProduct.Sort();

            for (int i = 0; i < idProduct.Count; i++)
            {
                Object[] param = new Object[1];
                param[0] = idProduct[i];
                quantiteProduct++;
                SqlAccess sql = new SqlAccess();
                DataSet data = sql.getProductById(param);

                if ((i < idProduct.Count - 1 && idProduct[i] != idProduct[i + 1]) || (i == idProduct.Count - 1))
                {
                    for (int table = 0; table < data.Tables.Count; table++)
                    {
                        DataTable dataTable = data.Tables[table];
                        for (int row = 0; row < dataTable.Rows.Count; row++)
                        {
                            dataString = new string[dataTable.Columns.Count+1];
                            for (int column = 0; column < dataTable.Columns.Count; column++)
                            {
                                dataString[column] = dataTable.Rows[row][column].ToString();
                            }
                            sumCommand += int.Parse(dataTable.Rows[row][3].ToString()) * quantiteProduct;
                        }
                    }

                    dataString[dataString.Length - 1] = quantiteProduct.ToString();
                    productItemBasket item = (productItemBasket)LoadControl("~/customer/productItemBasket.ascx");
                    basket.Controls.Add(item);
                    item.setData(dataString);
                    quantiteProduct = 0;
                }
            }
            prixCommand.Text = sumCommand.ToString();
        }

        protected void cmdBtm_Click(object sender, EventArgs e)
        {
            if (((List<int>)Session["basket"]).Count != 0)
            {
                SqlAccess sql = new SqlAccess();
                Object[] param = new Object[4];
                DateTime now = DateTime.Now;
                //////////////////////////////////
                //mettre à jour date, prix, custommer, adresse
                param[0] = now.Date;
                //prix
                param[1] = int.Parse(prixCommand.Text);
                //id_customer
                param[2] = (int)(Session["idCustomer"]);
                //id_adress
                param[3] = ((DataSet)sql.getAdressByCustomer((int)Session["idCustomer"])).Tables[0].Rows[0][0];
                sql.addCommand(param);

                //ajout command_product
                param = new Object[2];
                param[0] = sql.getLastIdCommand();
                List<int> idProduct = (List<int>)Session["basket"];
                for (int i = 0; i < idProduct.Count; i++)
                {
                    param[1] = idProduct[i];
                    sql.addCommandProduct(param);

                }

                ((List<int>)Session["basket"]).Clear();
                Server.ScriptTimeout = 2000;
                Response.Redirect("DisplayProduct.aspx");
            }

        }
    }
}